package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;


import model.Duty;
import model.Teacher;
import dao.DutyDAO;

public class DutyDAOImpl implements DutyDAO {
	private DBUtil dbu = new DBUtil();
	private Connection conn = null;
	private PreparedStatement pstat = null;
	private ResultSet rs = null;
	public List<Duty> getAllDuty() {

		List <Duty> list=new ArrayList<Duty>();
		try {
			conn = dbu.getConnection();
			String sql="select * from duty";
			pstat=conn.prepareStatement(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				Duty duty = new Duty();
				duty.setId(rs.getInt("id"));
				duty.setName(rs.getString("name"));
				list.add(duty);
			}
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}

	public Duty getDutyById(int id) {

		conn= dbu.getConnection();
		Duty duty = new Duty();
		try {
			pstat=conn.prepareStatement("select * from duty where id="+id);
			rs=	pstat.executeQuery();
			while(rs.next()){
			duty.setId(rs.getInt("id"));
			duty.setName(rs.getString("name"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return duty;
	}

	public Duty getDutyByName(String name) {
		conn= dbu.getConnection();
		Duty duty = new Duty();
		try {
			pstat=conn.prepareStatement("select * from duty where name='"+name+"'");
			rs=	pstat.executeQuery();
			while(rs.next()){
			duty.setId(rs.getInt("id"));
			duty.setName(rs.getString("name"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		return duty;
	}

	public int getDutyID(String name) {
		conn= dbu.getConnection();
		int id=0;
		try {
			pstat=conn.prepareStatement("select id from duty where name='"+name+"'");
			rs=	pstat.executeQuery();
			while(rs.next()){
			id=rs.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbu.closeConnection(conn, pstat, rs);
		}
		
		return id;
	}

}
